package lianxiyong.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public interface Userservice {
    boolean register(Test_3User user);
    boolean login(String username,String password) ;
}
class Test_3User {
    private String name;
    private   String password;
    private String username;
    public  Test_3User(String name,String username,String password){
        this.name=name;this.username=username;this.password=password;
    }



    public String getName(){
        return  name;
    }
    public String getPassword(){
        return password;
    }
    public String getUsername(){
        return username;
    }

}
class serve  implements Userservice {
    String username;

    public serve(Test_3User user) {
        this.username = user.getName();

    }

    String s = username + ".txt";

    @Override
    public boolean register(Test_3User user) {
        File a = new File("D:/" + s);
        if(!a.exists()) {
            try {
                a.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else return  false;
        FileOutputStream sb =null;
        try {
            sb = new FileOutputStream(a);
            byte[] l = user.getUsername().getBytes(StandardCharsets.UTF_8);
            byte[] bytes = user.getPassword().getBytes(StandardCharsets.UTF_8);
            sb.write(l);
            sb.write(bytes);
            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        finally {
            try{ if (sb != null) {
                sb.close();
            }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean login(String username, String password) {
        File file = new File("D:/" + s);
        FileInputStream p=null;
        try {
            p = new FileInputStream("D:/" + s);
            byte[] op = new byte[1024];
            int len = 0;
            String q = "";
            while ((len = p.read(op)) != -1) {
                q += new String(op, 0, len);
                //  System.out.println(q);
            }
            String[] r = q.split(" ");
            System.out.println(r[0] + " " + r[1]);
            System.out.println(username + " " + password);
            if (!r[0].equals(username) || !r[1].equals(password)) return false;

            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                if (p!=null) p.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
class lain {
    public static void main(String[] args) {
        Test_3User   user = new Test_3User("Johnlldet Doe", "johndoe123 ", "password123");
        serve a=new serve(user);
        // 注册用户
        if (a.register(user)) {
            System.out.println("User registered successfully.");
        } else {
            System.out.println("Failed to register user.");
        }

        // 登录用户
        if (a.login("johndoe123" , "password123")) {
            System.out.println("User logged in successfully.");
        } else {
            System.out.println("Login failed. Invalid username or password.");
        }
    }
}